package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.activity.client.SeckillGoodsFeignClient;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.order.OrderDetail;
import com.atguigu.gmall.model.user.UserAddress;
import com.atguigu.gmall.user.client.UserAddressFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 秒杀管理
 */
@Controller
public class SeckillGoodsController {


    @Autowired
    private SeckillGoodsFeignClient seckillGoodsFeignClient;
    @Autowired
    private UserAddressFeignClient userAddressFeignClient;

    //请求 回显秒杀列表页面  （秒杀频道首页）
    @GetMapping("/seckill.html")
    public String seckill(Model model){
        //查询今天所有秒杀的商品
        Result result = seckillGoodsFeignClient.findAll();
        model.addAttribute("list",result.getData());
        return "seckill/index";
    }
    //回显商品详情页面  立即秒杀
    @GetMapping("/seckill/{skuId}.html")
    public String detail(@PathVariable(name = "skuId") Long skuId,Model model){
        Result result = seckillGoodsFeignClient.getSeckillGoods(skuId);
        model.addAttribute("item",result.getData());
        return "seckill/item";
    }
    //去排队页面  /seckill/queue.html
    @GetMapping("/seckill/queue.html")
    public String queue(@RequestParam(name = "skuId") String skuId,
                        @RequestParam(name = "skuIdStr") String skuIdStr, Model model){
        model.addAttribute("skuId",skuId);
        model.addAttribute("skuIdStr",skuIdStr);
        return "seckill/queue";
    }
    //抢购成功之后  去下单
    @GetMapping("/seckill/trade.html")
    public String seckillTrade(HttpServletRequest request,Model model){
        String userId = AuthContextHolder.getUserId(request);
        //1:当前用户的收货地址
        List<UserAddress> userAddressList =
                userAddressFeignClient.findUserAddressListByUserId(Long.parseLong(userId));
        request.setAttribute("userAddressList",userAddressList);
        //2:商品清单  只有一款商品  秒杀商品  只有一件  商品数量  商品总金额
        Result<Map> result = seckillGoodsFeignClient.getOrderDetail(userId);
        model.addAllAttributes(result.getData());
        //去结算页面
        return "seckill/trade";
    }
}
